package bb.lanxing.adapter;

import android.text.format.Formatter;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import bb.lanxing.R;
import bb.lanxing.lib.devices.core.sync.DeviceFile;
import bb.lanxing.lib.devices.core.sync.FitDeviceFile;
import bb.lanxing.util.CommonUtil;
import bb.lanxing.util.DateUtil;
import java.lang.ref.WeakReference;
import java.util.List;

public class DeviceFileListAdapter extends RecyclerView.Adapter<DeviceFileListAdapter.BrytonLogVH>
        implements View.OnCreateContextMenuListener {
    List<DeviceFile> mDeviceRecords;
    WeakReference<DeviceFileAdapterListener> mListenerRef;
    private RecyclerView mRecyclerView;

    public interface DeviceFileAdapterListener {
        boolean canResync();

        MenuInflater getMenuInflater();

        int getState(long j);

        boolean resync(DeviceFile deviceFile);

        void syncItem(DeviceFile deviceFile);
    }

    public DeviceFileListAdapter(List<DeviceFile> list, DeviceFileAdapterListener deviceFileAdapterListener) {
        this.mDeviceRecords = list;
        this.mListenerRef = new WeakReference<>(deviceFileAdapterListener);
    }

    @Override
    public BrytonLogVH onCreateViewHolder(ViewGroup viewGroup, int i) {
        return new BrytonLogVH(LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.item_bryton_log_file, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(BrytonLogVH brytonLogVH, int i) {
        final DeviceFile deviceFile = this.mDeviceRecords.get(brytonLogVH.getAdapterPosition());
        if (!(deviceFile instanceof FitDeviceFile)) {
            return;
        }
        FitDeviceFile fitDeviceFile = (FitDeviceFile) deviceFile;
        String format = DateUtil.format0.format(fitDeviceFile.getStartTime());
        String str = null;
        if (fitDeviceFile.getDistance() > 0) {
            str = CommonUtil.getKMDistance(fitDeviceFile.getDistance() / 1000.0d) + "Km";
        } else if (fitDeviceFile.getSize() > 0) {
            str = Formatter.formatFileSize(brytonLogVH.itemView.getContext(), fitDeviceFile.getSize());
        } else if (fitDeviceFile.getDuration() > 0) {
            str = DateUtil.format2.format(fitDeviceFile.getDuration());
        }
        brytonLogVH.titleView.setText(format);
        brytonLogVH.durationView.setText(str);
        DeviceFileAdapterListener deviceFileAdapterListener = this.mListenerRef.get();
        if (deviceFileAdapterListener != null) {
            int state = deviceFileAdapterListener.getState(fitDeviceFile.getId());
            if (state == 1) {
                brytonLogVH.stateView.setText(R.string.str_sync_import_data);
            } else if (state == 2) {
                brytonLogVH.stateView.setText(R.string.str_sync_parse_fit_file);
            } else if (state == 3) {
                brytonLogVH.stateView.setText(R.string.str_sync_already_exists);
            } else if (state == 4) {
                brytonLogVH.stateView.setText(R.string.str_sync_failed);
            } else {
                brytonLogVH.stateView.setText(R.string.str_sync_not_exists);
            }
        }
        brytonLogVH.itemView.setOnCreateContextMenuListener(this);
        brytonLogVH.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DeviceFileAdapterListener deviceFileAdapterListener2 = DeviceFileListAdapter.this.mListenerRef.get();
                if (deviceFileAdapterListener2 != null) {
                    deviceFileAdapterListener2.syncItem(deviceFile);
                }
            }
        });
    }

    @Override
    public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
        int childAdapterPosition;
        final DeviceFileAdapterListener deviceFileAdapterListener = this.mListenerRef.get();
        if (deviceFileAdapterListener == null || !deviceFileAdapterListener.canResync() || (childAdapterPosition = this.mRecyclerView.getChildAdapterPosition(view)) == -1) {
            return;
        }
        deviceFileAdapterListener.getMenuInflater().inflate(R.menu.menu_sync_options, contextMenu);
        final DeviceFile deviceFile = this.mDeviceRecords.get(childAdapterPosition);
        if (!deviceFileAdapterListener.canResync()) {
            return;
        }
        MenuItem findItem = contextMenu.findItem(R.id.menu_sync_retry);
        findItem.setVisible(true);
        findItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                deviceFileAdapterListener.resync(deviceFile);
                return true;
            }
        });
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.mRecyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.mRecyclerView = null;
    }

    @Override
    public long getItemId(int i) {
        return this.mDeviceRecords.get(i).getId() * 1000;
    }

    public void updateSyncState(long j, String str) {
        BrytonLogVH brytonLogVH;
        int positionById = getPositionById(j);
        if (positionById == -1 || (brytonLogVH = (BrytonLogVH) this.mRecyclerView.findViewHolderForAdapterPosition(positionById)) == null) {
            return;
        }
        brytonLogVH.stateView.setText(str);
    }

    public int getPositionById(long j) {
        int i = 0;
        while (true) {
            List<DeviceFile> list = this.mDeviceRecords;
            if (list == null || i >= list.size()) {
                return -1;
            }
            if (this.mDeviceRecords.get(i).getId() == j) {
                return i;
            }
            i++;
        }
    }

    @Override
    public int getItemCount() {
        List<DeviceFile> list = this.mDeviceRecords;
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    public static class BrytonLogVH extends RecyclerView.ViewHolder {
        public TextView distanceView;
        public TextView durationView;
        public ImageView imageView;
        public TextView stateView;
        public TextView titleView;

        public BrytonLogVH(View view) {
            super(view);
            this.titleView = (TextView) view.findViewById(R.id.nameView);
            this.durationView = (TextView) view.findViewById(R.id.sizeView);
            this.stateView = (TextView) view.findViewById(R.id.stateView);
            this.imageView = (ImageView) view.findViewById(R.id.imageView);
        }
    }
}
